// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成

package com.fowo.api.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.common.annotaion.DataObjectType;
import com.fowo.api.common.excel.TemplateModelBuildEventListener;
import com.fowo.api.common.model.ImportRow;
import com.fowo.api.common.util.SheetUtils;
import com.fowo.api.controller.purchase.importPo.StockOutProductImportPo;
import com.fowo.api.entity.AmazonAdvertiserInvoices;
import com.fowo.api.entity.AmazonAdvertiserInvoicesCampaign;
import com.fowo.api.model.amazon.advertiser.invoices.campaign.AmazonAdvertiserInvoicesCampaignImportPo;
import com.fowo.api.model.listing.father.ListingPairBo;
import com.fowo.api.model.stock.out.product.StockOutProductItemVo;
import com.fowo.api.service.AmazonAdvertiserInvoicesCampaignService;
import com.fowo.api.sys.component.ExecuteScriptEngine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static com.alibaba.excel.EasyExcelFactory.read;

/**
 * 广告发票记录 服务实现类
 * @author 蜂窝蜜造平台 配置人：鹿丙涛
 */
@Service
@Slf4j
@DataObjectType(AmazonAdvertiserInvoices.class)
@DS("ds24")
public class AmazonAdvertiserInvoicesServiceImpl extends AmazonAdvertiserInvoicesBaseServiceImpl {

    @Resource
    private AmazonAdvertiserInvoicesCampaignServiceImpl amazonAdvertiserInvoicesCampaignService;

  /**
   * 导入承担商品
   */
  @Transactional(rollbackFor = Exception.class)
  @Override
  public void importItems(MultipartFile file) throws Exception {
    InputStream in = file.getInputStream();
    List<AmazonAdvertiserInvoicesCampaignImportPo> list = new ArrayList<>();
    // 读取 Excel 到 rows
    EasyExcel
          .read(in)
          .useDefaultListener(false)
          .registerReadListener(
              new TemplateModelBuildEventListener<>(AmazonAdvertiserInvoicesCampaignImportPo.class, null) {
                @Override
                public void invokeRow(AmazonAdvertiserInvoicesCampaignImportPo row, AnalysisContext cxt) {

                  list.add(row);
                }
              }
          )
          .sheet()
          .doRead();
    List<AmazonAdvertiserInvoicesCampaign> updateList = new ArrayList<>();
    for(AmazonAdvertiserInvoicesCampaignImportPo po : list){
        final AmazonAdvertiserInvoicesCampaign campaign = amazonAdvertiserInvoicesCampaignService.getOne(Wrappers.lambdaQuery(AmazonAdvertiserInvoicesCampaign.class)
                .eq(AmazonAdvertiserInvoicesCampaign::getInvoiceId, po.getInvoiceId())
                .eq(AmazonAdvertiserInvoicesCampaign::getCampaignId, po.getCampaignId()));
        if(campaign != null && StrUtil.isBlank(campaign.getItems())){
            AmazonAdvertiserInvoicesCampaign update = new AmazonAdvertiserInvoicesCampaign();
            update.setId(campaign.getId());
            update.setItems(po.getItems());
            updateList.add(update);
        }
    }
    if(CollectionUtil.isNotEmpty(updateList)){
        amazonAdvertiserInvoicesCampaignService.updateBatchById(updateList);
    }
  }


}
